<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Variety_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 得到所有小类信息
	 */
	public function find_all_varieties(){
		$this->db->select('t_variety.id,t_variety.name,t_variety.categoryid,t_category.name as catname,t_variety.position,show');
		$this->db->from('t_variety');
		$this->db->join('t_category', 't_category.id = t_variety.categoryid','left');
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据大类ID得到该大类下的小类信息
	 */
	public function find_varieties_by_cat($categoryid){
	    $this->db->where('categoryid',$categoryid);
		$query = $this->db->get('t_variety');
		return $query->result_array();
	}
	/*
	 * 根据ID得到小类信息
	 */
	public function load($id){
		$this->db->select('t_variety.id,t_variety.name,t_variety.categoryid,t_category.name as catname');
		$this->db->from('t_variety');
		$this->db->where('t_variety.id',$id);
		$this->db->join('t_category', 't_category.id = t_variety.categoryid','left');
		$query = $this->db->get();
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加小类信息
	 */
	public function create($variety){
		$this->db->insert('t_variety', $variety);
		return $this->db->insert_id();
	}
	/*
	 * 更新小类信息
	 */
	public function update($id,$variety){
		$this->db->where('id', $id);
		$this->db->update('t_variety', $variety);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除小类信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_variety');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据大类ID查询大类下是否有小类(暂时没用)
	 */
	public function check_variety_by_cat($categoryid){
		$this->db->select('id');
		$query = $this->db->get_where('t_variety',array('categoryid' => $categoryid));
		return $query->row_array();
	}
}